package com.eastmoney.emis.edda.infrastructure.util;

import com.eastmoney.emis.edda.infrastructure.report.Report;
import org.springframework.util.StreamUtils;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipUtils {

    /**
     * @param input .zip文件内容
     * @return 解压之后的文件内容
     */
    public static List<Report> unzip(byte[] input) throws IOException {
        ZipInputStream zipInputStream = new ZipInputStream(new ByteArrayInputStream(input));
        try {
            List<Report> reports = new ArrayList<>();
            ZipEntry zipEntry = null;
            while ((zipEntry = zipInputStream.getNextEntry()) != null) {
                Report report = new Report();
                report.setName(zipEntry.getName());
                report.setData(StreamUtils.copyToByteArray(zipInputStream));
                reports.add(report);
            }
            return reports;
        } finally {
            zipInputStream.close();
        }

    }
}
